package com.cjh.wechat.core.service.impl;

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import com.cjh.wechat.core.service.RedisService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Service
public class RedisServiceImpl implements RedisService
{
    private String    hostname = "127.0.0.1";
    private int       port     = 6379;
    private JedisPool jedisPool;
    
    @PostConstruct
    public void init()
    {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(5);
        config.setMaxTotal(300);
        config.setMaxWaitMillis(100);
        config.setTestOnBorrow(false);
        // 最长可以忍受单次读取10秒钟的开销
        jedisPool = new JedisPool(config, hostname, port, 10000);
    }
    
    @Override
    public Jedis get()
    {
        return jedisPool.getResource();
    }
    
}
